package org.zhang.core.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonUtil<T> {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private JacksonUtil() {
        // 私有构造函数，防止外部实例化
    }

    public static ObjectMapper getInstance() {
        return objectMapper;
    }

    public static String toJson(Object src) {
        try {
            return getInstance().writeValueAsString(src);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static <T> T fromJson(String json, Class<T> classOfT) {
        try {
            return getInstance().readValue(json, classOfT);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

}
